<template>
  <el-card shadow="never" class="aui-card--fill">
    <div class="mod-wms__station}">
      <div class="ba-operate-form">
        <el-form :model="dataForm" @keyup.enter.native="getDataList()" label-width="70px">
          <el-row :gutter="20">
            <el-col :md="6" :sm="9">
              <el-form-item label="站台编号">
                <el-input v-model="dataForm.stationNo" placeholder="模糊匹配" maxlength="20" clearable></el-input>
              </el-form-item>
            </el-col>
            <el-col :md="6" :sm="9">
              <el-form-item label="站台类型">
                <el-input v-model="dataForm.stationType" placeholder="精确匹配" maxlength="20" clearable></el-input>
              </el-form-item>
            </el-col>
            <el-col :md="6" :sm="9">
              <el-form-item label="站台描述">
                <el-input v-model="dataForm.stationDescription" placeholder="模糊匹配" maxlength="20" clearable></el-input>
              </el-form-item>
            </el-col>
            <el-col :md="6" :sm="9">
              <div class="right-handle-group">
                <el-button-group>
                  <el-button @click="getDataList()" type="primary">{{ $t('query') }}</el-button>
                  <el-button @click="getReset()">{{ $t("reset") }}</el-button>
                </el-button-group>
              </div>
            </el-col>
          </el-row>
        </el-form>
      </div>

      <el-table v-loading="dataListLoading" :data="dataList" border @selection-change="dataListSelectionChangeHandle"
        style="width: 100%">
        <el-table-column prop="stationId" label="id" header-align="center" align="center" sortable></el-table-column>
        <el-table-column prop="stationNo" label="站台编号" header-align="center" align="left"
          min-width="150"></el-table-column>
        <el-table-column prop="stationType" label="站台类型" header-align="center" align="center"></el-table-column>
        <el-table-column prop="stationFloor" label="所属楼层" header-align="center" align="center"></el-table-column>
        <el-table-column prop="stationDescription" label="站台描述" header-align="center" align="left"
          show-overflow-tooltip></el-table-column>
        <el-table-column prop="status" label="可用状态" header-align="center" align="center">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.status" @change="changeStatus($event, scope.row)" active-color="#13ce66"
              inactive-color="#ff4949" :active-value="0" :inactive-value="1"></el-switch>
          </template>
        </el-table-column>
        <el-table-column prop="creatorName" label="创建用户" header-align="center" align="center"
          show-overflow-tooltip></el-table-column>
        <el-table-column prop="createDate" label="创建日期" header-align="center" align="center" min-width="155"
          sortable></el-table-column>
        <el-table-column prop="updaterName" label="更新用户" header-align="center" align="center"
          show-overflow-tooltip></el-table-column>
        <el-table-column prop="updateDate" label="更新日期" header-align="center" align="center" min-width="155"
          sortable></el-table-column>
        <el-table-column :label="$t('handle')" fixed="right" header-align="center" align="center" width="80">
          <template slot-scope="scope">
            <el-button v-if="$hasPermission('wms:station:update')" type="text" size="small"
              @click="addOrUpdateHandle(scope.row.stationId)">{{ $t("update") }}</el-button>
            <el-button v-if="$hasPermission('wms:station:delete')" type="text" size="small"
              @click="deleteHandle(scope.row.stationId)">{{ $t("delete") }}</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination :current-page="page" :page-sizes="[10, 20, 50, 100]" :page-size="limit" :total="total"
        layout="total, sizes, prev, pager, next, jumper" @size-change="pageSizeChangeHandle"
        @current-change="pageCurrentChangeHandle">
      </el-pagination>
      <!-- 弹窗, 新增 / 修改 -->
      <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    </div>
  </el-card>
</template>

<script>
import mixinViewModule from "@/mixins/view-module";
import AddOrUpdate from "./station-add-or-update";
import debounce from 'lodash/debounce'
export default {
  mixins: [mixinViewModule],
  data() {
    return {
      mixinViewModuleOptions: {
        getDataListURL: "/wms/station/page",
        getDataListIsPage: true,
        exportURL: "/wms/station/export",
        deleteURL: "/wms/station",
        deleteIsBatch: true,
      },
      dataForm: {
        stationNo: "",
        stationType: '',
        stationDescription: '',
      },
      changeForm: {
        stationId: '',
        status: ''
      }
    };
  },
  components: {
    AddOrUpdate,
  },
  methods: {
    //0.重置搜索框
    getReset() {
      this.dataForm = this.$options.data().dataForm;
    },
    changeStatus: debounce(function (val, row) {
      var tempStatus = row.status === 0 ? 1 : 0;
      this.changeForm.stationId = row.stationId;
      this.changeForm.status = row.status;
      this.$http[!this.changeForm.stationId ? 'post' : 'put']('/wms/station/', this.changeForm).then(({ data: res }) => {
        if (res.code !== 0) {
          //如果更新失败则重置为原来的状态
          row.status = tempStatus;
          return this.$message.error(res.msg)
        }
        this.$message({
          message: this.$t('prompt.success'),
          type: 'success',
          duration: 500,
          onClose: () => {
          }
        })
      }).catch(() => { })
    })
  }
};
</script>
